package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Likes;
import org.csu.secondhand.api.persistence.LikesMapper;
import org.csu.secondhand.api.service.LikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("likesService")
public class LikesServiceImpl implements LikesService {
    @Autowired
    LikesMapper likesMapper;

    @Override
    public Likes doLike(Likes likes) {
        likesMapper.insert(likes);
        return likes;
    }

    @Override
    public Likes dontLike(String username, long posting) {
        QueryWrapper<Likes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("posting", posting);
        Likes likes = likesMapper.selectOne(queryWrapper);
        likesMapper.delete(queryWrapper);
        return likes;
    }

    @Override
    public int doILikeIt(String username, long posting) {
        QueryWrapper<Likes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("posting", posting);
        Likes likes = likesMapper.selectOne(queryWrapper);
        if(likes == null)
            return 0;
        return 1;
    }
}
